package com.fjwt.gz.mgr.ctrl.agent;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AgentEmployeeDTO;
import com.fjwt.gz.db.entity.AgentEmployeeEntity;
import com.fjwt.gz.db.entity.FansEntity;
import com.fjwt.gz.db.vo.AgentEmployeeVO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AgentEmployeeService;
import com.fjwt.gz.service.impl.FansService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 机构人员管理
 */
@RestController
@Slf4j
@RequestMapping("/api/agent/employee")
public class AgentEmployeeController extends CommonCtrl {

    @Autowired
    private AgentEmployeeService agentEmployeeService;

    @Autowired
    private FansService fansService;

    /**
     * 机构员工列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_PROXY_EMPLOYEE_INFO')")
    @PostMapping("/list")
    public ApiRes list(){
        AgentEmployeeDTO dto = getObject(AgentEmployeeDTO.class);
        String currentAgentNo= getCurrentUser().getSysUser().getBelongInfoId();
        dto.setCurrentAgentNo(currentAgentNo);
        log.info("pc端查询机构员工列表，请求地址：/api/agent/employee/list，请求地址：{}", JSONObject.toJSONString(dto));
        IPage<AgentEmployeeVO> page = agentEmployeeService.queryList(getIPage(), dto);
        log.info("pc端查询机构员工列表，请求地址：/api/agent/employee/list，返回结果：{}", JSONObject.toJSONString(page));
        return ApiRes.page(page);
    }

    /**
     * 根据机构号查询机构员工列表
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_ACTIVITY_LIST_INFO_ADD_QR')")
    @PostMapping("/getListByAgentNo")
    public ApiRes getListByAgentNo() {
        AgentEmployeeDTO dto = getObject(AgentEmployeeDTO.class);
        log.info("pc端查询根据机构号查询机构员工列表，请求地址：/api/agent/employee/getListByAgentNo，请求参数：{}", JSONObject.toJSONString(dto));
        List<AgentEmployeeEntity> list = agentEmployeeService.getListByAgentNo(dto);
        log.info("pc端查询根据机构号查询机构员工列表，请求地址：/api/agent/employee/getListByAgentNo，返回结果：{}", JSONObject.toJSONString(list));
        return ApiRes.ok(list);
    }

    /**
     * 批量增加机构员工
     */
    @PreAuthorize("hasAnyAuthority('ENT_PROXY_EMPLOYEE_INFO_IMPT')")
    @PostMapping("/addBatch")
    public ApiRes addBatch(){
        AgentEmployeeDTO agentEmployeeDTO = getObject(AgentEmployeeDTO.class);
        String currentAgentNo = getCurrentUser().getSysUser().getBelongInfoId();
        agentEmployeeDTO.setCurrentAgentNo(currentAgentNo);
        log.info("pc端批量增加机构员工，请求地址：/api/agent/employee/addBatch，请求地址：{}", JSONObject.toJSONString(agentEmployeeDTO));
        String failEmployeeUrl = agentEmployeeService.addByBatch(agentEmployeeDTO, OssFileConfig.BIZ_TYPE.UPLOAD);
        log.info("pc端批量增加机构员工，请求地址：/api/agent/employee/addBatch，返回结果：{}", failEmployeeUrl);
        return ApiRes.ok(failEmployeeUrl);
    }

    /**
     * 机构员工列表导出
     */
    @PreAuthorize("hasAnyAuthority('ENT_PROXY_EMPLOYEE_INFO_EXPT')")
    @PostMapping("/export")
    public ApiRes export(HttpServletResponse response){
        AgentEmployeeDTO dto = getObject(AgentEmployeeDTO.class);
        AgentEmployeeDTO vo = new AgentEmployeeDTO();
        BeanUtils.copyProperties(dto,vo);
        log.info("pc端查询机构员工列表，请求地址：/api/agent/employee/export，请求地址：{}", JSONObject.toJSONString(dto));
        IPage<AgentEmployeeVO> page = agentEmployeeService.queryList(new Page(0, -1), vo);
        log.info("pc端查询机构员工列表，请求地址：/api/agent/employee/export，返回结果：{}", JSONObject.toJSONString(page));
        //导出数据
        return ApiRes.ok(agentEmployeeService.export(page,response));
    }
    /**
     * 机构员工展业权限绑定解绑
     */
    @PreAuthorize("hasAnyAuthority('ENT_PROXY_EMPLOYEE_INFO_APP')")
    @PostMapping("/bindBusinessPermission")
    public ApiRes bindBusinessPermission(){
        Long employeeId = getValLong("employeeId");
        String appId = getValString("appId");
        Integer isBind = getValInteger("isBind");//0绑定1解绑
        String payOpenId = getValString("payOpenId");
        log.info("pc端机构员工展业权限绑定解绑，请求地址：/api/agent/employee/bindBusinessPermission，机构员工号：{}，是否绑定{},小程序appid:{}",employeeId,isBind,appId,payOpenId);
        agentEmployeeService.bindBusinessPermission(employeeId,appId,isBind,payOpenId);
        return ApiRes.ok();
    }

    /**
     * 获取会员信息
     */
    @PreAuthorize("hasAnyAuthority('ENT_PROXY_EMPLOYEE_INFO_APP')")
    @PostMapping("/getFansInfo")
    public ApiRes getFansInfo(){
        String agentEmployeeId = getValString("agentEmployeeId");
        log.info("pc端获取机构会员信息，请求地址：/api/agent/employee/getFansInfo，机构员工号：{}",agentEmployeeId);
        AgentEmployeeEntity byId = agentEmployeeService.getById(agentEmployeeId);
        List<FansEntity> fansEntities = fansService.lambdaQuery().eq(FansEntity::getPhone, byId.getEmployeePhone()).list();
        return ApiRes.ok(fansEntities);
    }

}

